package ljl.alg.jianzhioffer.round3.easy;

public class _39_majorityElement {

    class mine {
        public int majorityElement(int[] nums) {
            int n = nums[0], count = 0;
            for (int num : nums) {
                if (count == 0) {
                    n = num;
                    count = 1;
                } else if (num == n) {
                    count++;
                } else {
                    count--;
                }
            }
            return n;
        }
    }

    /**
     * 路飞的更简短一点
     * */
    class lufei {

        public int majorityElement(int[] nums) {
            int x = 0, votes = 0;
            for(int num : nums){
                if(votes == 0) x = num;
                votes += num == x ? 1 : -1;
            }
            return x;

        }
    }
}

